package com.example.iot.controller;

import com.example.iot.entity.DeviceArchives;
import com.example.iot.entity.PatchUpgrade;
import com.example.iot.service.impl.PatchUpgradeServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/patchupgrade")
@RequiredArgsConstructor
@Component
public class patchUpgradeController {
    private final PatchUpgradeServiceImpl patchUpgradeService;
    @PostMapping("/patchupgrade")
    public void patchUpgrade (@RequestBody PatchUpgrade patchUpgrade) {
        if(patchUpgrade.getDeviceid()!=null){
            PatchUpgrade patchUpgrade1=patchUpgradeService.selectByDevID(patchUpgrade.getDeviceid());
            if(patchUpgrade1!=null){
                patchUpgrade.setId(patchUpgrade1.getId());
                patchUpgradeService.updateById(patchUpgrade);
            }else{
                patchUpgradeService.save(patchUpgrade);
            }

        }else{
            patchUpgradeService.updateById(patchUpgrade);
        }

    }
    @GetMapping("/deviceverify")
    public PatchUpgrade  selectByDevID (String deviceId) {
        PatchUpgrade patchUpgrade=patchUpgradeService.selectByDevID(deviceId);
        if(patchUpgrade!=null&&patchUpgrade.getIsipgrade()==2){
            return patchUpgrade;
        }
        return null;
    }
}
